package com.fudan.backend.controller;

import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.StoreOpenRequest;
import com.fudan.backend.service.StoreOpenRequestService;
import com.fudan.backend.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/store")
public class StoreOpenRequestController {
    @Autowired
    StoreOpenRequestService storeOpenRequestService;

    @PostMapping("/open-request")
    public R addStoreOpenRequest(@RequestBody StoreOpenRequest storeOpenRequest) {
        RetCode retCode = storeOpenRequestService.addStoreOpenRequest(storeOpenRequest);
        if (retCode != RetCode.STORE_OPEN_REQUEST_SUCCESS) {
            return new R(retCode.getCode(), retCode.getMessage());
        } else {
            return new R(retCode.getCode(), retCode.getMessage(), retCode.getMessage());
        }
    }

    @GetMapping("/open-request")
    public R getAllStoreOpenRequest(int current, int size) {
        PageInfo<StoreOpenRequest> page = storeOpenRequestService.getAllStoreOpenRequest(current, size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("openRequestList", page.getList());
        map.put("total", page.getTotal());
        return new R(RetCode.STORE_OPEN_REQUEST_SUCCESS.getCode(), RetCode.STORE_OPEN_REQUEST_SUCCESS.getMessage(), RetCode.STORE_OPEN_REQUEST_SUCCESS.getMessage(), map);
    }

    @GetMapping("/open-request/{openRequestId}")
    public R getStoreOpenRequestByStoreRequestId(@PathVariable("openRequestId") String storeOpenRequestId) {
        StoreOpenRequest storeOpenRequest = storeOpenRequestService.getStoreOpenRequestByStoreOpenRequestId(storeOpenRequestId);
        return new R(RetCode.STORE_OPEN_REQUEST_SUCCESS.getCode(), RetCode.STORE_OPEN_REQUEST_SUCCESS.getMessage(), RetCode.STORE_OPEN_REQUEST_SUCCESS.getMessage(), storeOpenRequest);
    }

    @PostMapping("/open-request/process")
    public R processStoreOpenRequest(@RequestBody StoreOpenRequest storeOpenRequest) {
        RetCode retCode = storeOpenRequestService.processStoreOpenRequest(storeOpenRequest);
        return new R(retCode.getCode(), retCode.getMessage(), retCode.getMessage());
    }

    @GetMapping("/open-request/seller/{userId}")
    public R getAllStoreOpenRequestByUserId(int current, int size, @PathVariable("userId") String userId) {
        PageInfo<StoreOpenRequest> page = storeOpenRequestService.getAllStoreOpenRequestByUserId(current, size, userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("storeOpenRequestList", page.getList());
        map.put("total", page.getTotal());
        return new R(RetCode.STORE_OPEN_REQUEST_SUCCESS, map);
    }
}
